/**
  ******************************************************************************
  * Copyright © Beijing WIIP Co., Ltd. 2022-2024. All rights reserved.
  ******************************************************************************
  * @file: app.c
  * @brief: 顶层应用业务流程
  *
  ******************************************************************************
  * @note:
  *
  ******************************************************************************
  * Change Logs:
  * Date           Author       Notes
  * 2024-07-25     Edward       the first version
  *
  *
  ******************************************************************************
  */

/* 声明包含头文件 --------------------------------------------------------------*/
#include "../03_FML/OS/fml_task.h"
#include "../02_BLL/Inc/task_HMI.h"

#include "../02_BLL/Inc/task_dataRW.h"
#include "../02_BLL/Inc/task_communication.h"
#include "../02_BLL/Inc/task_dataRW.h"
#include "../02_BLL/Inc/task_communication.h"
#include "../02_BLL/Inc/task_dataExport.h"
#include "../02_BLL/Inc/task_RTC.h"
//#include "../02_BLL/Inc/task_RGB.h"
#include "../02_BLL/Inc/task_beep.h"
#include "../02_BLL/Inc/task_backlight.h"

/* 声明私有类型定义--------------------------------------------------------------*/
uint8_t cdcount = 0;
/* 声明私有变量 ---------------------------------------------------------------*/

/* 声明私有常量 ---------------------------------------------------------------*/

/* 声明私有宏定义 --------------------------------------------------------------*/

/* 声明私有函数 ---------------------------------------------------------------*/

/**
  * @brief
  * @param  task
  * @retval None
  */
static int systeamInit(struct task_pcb *task)
{
	/* 系统初始化 （后续需要进行封装，放入DRI）*/
	/* 读取USB如果正确，读取字库图库并写入NAND中，如果读取错误则直接读取NANDFLASH中的字库图库屏幕参数 */
	g_RW_State = RW_READ_NAND_INIT;
//	g_RW_State = RW_READ_USB_MATERIAL;
	/* 外设初始化 （后续需要进行封装，放入DRI）*/

	/* 初始化结束，关闭此任务，启动其他任务 */
	task_stop(TASK1_INIT);
	task_start(TASK2_APP);
	task_start(TASK3_HMI);
	task_start(TASK4_COMMUNICATION);
	task_start(TASK5_DATA_RW);
	task_start(TASK6_DATA_EXPORT);
	task_start(TASK7_RTC);
//	task_start(TASK8_RGB);
	task_start(TASK9_BEEP);
	task_start(TASK10_BACKLIGHT);

	return 1;
}

/**
  * @brief	顶层任务逻辑
  * @param  task 本任务控制结构体
  * @retval None
  */
static int applicationFunc(struct task_pcb *task)
{

	/* 判断所有读写操作 */
	switch (g_RW_State)
	{
		case RW_READY:					        // 准备就绪
			break;
		case RW_READ_USB_MATERIAL:				// 读取USB素材
			task_event_send(TASK6_DATA_EXPORT, TASK6_EVENT1_USB_MATERIAL_READ);
			break;
		case RW_WRITE_USB_DATA:			        // 向USB中写入数据
			task_event_send(TASK6_DATA_EXPORT, TASK6_EVENT2_USB_DATA_WRITE);
			break;
		case RW_EXPORT_USB_DATA:
			g_packReadOK = 1;
			g_overWritten = 1;
			g_mountUSB_flag = 0;
			task_event_send(TASK6_DATA_EXPORT, TASK6_EVENT2_USB_DATA_WRITE);
			break;
		case RW_READ_NAND_INIT:
			task_event_send(TASK5_DATA_RW, TASK5_EVENT1_NAND_MATERIAL_READ);
			task_event_send(TASK5_DATA_RW, TASK5_EVENT1_NAND_SCREEN_READ);
			task_event_send(TASK5_DATA_RW, TASK5_EVENT1_NAND_ALARM_READ);
			break;
	   case RW_READ_NAND_CURVE:               // 读取NAND中的曲线数据
			task_event_send(TASK5_DATA_RW, TASK5_EVENT1_NAND_CURVE_READ);
			break;
		case RW_READ_NAND_PACK:				   // 读取NAND中的一包数据
			task_event_send(TASK5_DATA_RW, TASK5_EVENT1_NAND_PACK_READ);
			break;
		case RW_WRITE_NAND_MATERIAL:		   // 向NAND中写入素材
			task_event_send(TASK5_DATA_RW, TASK5_EVENT2_NAND_MATERIAL_WRITE);
			break;
		case RW_WRITE_NAND_SCREEN:             // 向NAND写入屏幕参数
			task_event_send(TASK5_DATA_RW, TASK5_EVENT2_NAND_SCREEN_WRITE);
			break;
		case RW_WRITE_NAND_ALARM:	          // 向NAND中写入报警记录
			task_event_send(TASK5_DATA_RW, TASK5_EVENT2_NAND_ALARM_WRITE);
			break;
		default:
			break;
	}
	if (secondFlag)
	{
		secondFlag = 0;
		task_event_send(TASK5_DATA_RW, TASK5_EVENT2_NAND_RUN_WRITE);
	}
	/* 判断是否有通讯写入数据的请求 */
	if(task->event & TASK2_EVENT1_COMM_WRITE)
	{
		task_event_send(TASK4_COMMUNICATION, TASK4_EVENT1_COMM_WRITE);	/* 发送通讯数据写入指令至通讯业务 */
		task->event &= (~TASK2_EVENT1_COMM_WRITE);
	}

	/* 判断通讯数据是否异常 */
	if(task->event & TASK2_EVENT2_COMM_ABNORMAL)
	{
		task_event_send(TASK3_HMI, TASK3_EVENT1_COMM_ABNORMAL);	/* 发送异常信号至触摸屏任务 */
		task_event_send(TASK8_RGB, TASK8_EVENT1_COMM_ABNORMAL);	/* 发送异常信号至RGB灯带任务 */
		task->event &= (~TASK2_EVENT2_COMM_ABNORMAL);
	}
	else if(task->event & TASK2_EVENT3_COMM_FRUSH)		/* 读取通讯数据是否刷新 */
	{
///		task_event_send(TASK5_DATA_RW, TASK5_EVENT1_COMM_FRUSH);	/* 通讯数据发布至数据读取存储 */
		task_event_send(TASK3_HMI, TASK3_EVENT2_COMM_FRUSH);		/* 通讯数据发布至触摸屏 */
		task_event_send(TASK8_RGB, TASK8_EVENT2_COMM_FRUSH);		/* 通讯数据发布至RGB灯带任务 */
		task->event &= (~TASK2_EVENT3_COMM_FRUSH);
	}

	/* 判断RTC修改请求 */
	if(task->event & TASK2_EVENT7_RTC_EDIT)
	{
		task_event_send(TASK7_RTC, TASK7_EVENT1_RTC_EDIT);	/* 发送RTC修改信号至RTC业务 */
		task->event &= (~TASK2_EVENT7_RTC_EDIT);
	}

	/* 100ms定时发送读取 */
	task_event_send(TASK7_RTC, TASK7_EVENT2_RTC_READ);	/* 发送RTC读取信号至RTC业务 */
//	if(secondFlag)
//	{
//		task_event_send(TASK7_RTC, TASK7_EVENT2_RTC_READ);	/* 发送RTC读取信号至RTC业务 */
//		secondFlag = 0;
//	}
	/* 读取触摸屏是否按下 */
	if(task->event & TASK2_EVENT8_HMI_TOUCH)
	{
		task_event_send(TASK9_BEEP, TASK9_EVENT1_HMI_TOUCH);	/* 发送蜂鸣器工作信号至蜂鸣器业务 */
		task->event &= (~TASK2_EVENT8_HMI_TOUCH);
	}

	/* 判断背光调节请求 */
	if(task->event & TASK2_EVENT9_HMI_BACKLIGHT)
	{
		task_event_send(TASK10_BACKLIGHT, TASK10_EVENT1_HMI_BACKLIGHT);	/* 发送背光调节信号至背光业务 */
		task->event &= (~TASK2_EVENT9_HMI_BACKLIGHT);
	}

	return 1;
}

/**
  * @brief
  * @param  task
  * @retval None
  */
void APP_runing(void)
{
	/* 初始化所有任务----索引-----回调函数--------间隔周期-------- */
	task_init(TASK1_INIT, systeamInit, TASK_PERIOD_TASK1);
	task_init(TASK2_APP, applicationFunc, TASK_PERIOD_TASK2);
	task_init(TASK3_HMI, bll_HMI, TASK_PERIOD_TASK3);
	task_init(TASK4_COMMUNICATION, bll_communication, TASK_PERIOD_TASK4);
	task_init(TASK5_DATA_RW, bll_dataRW, TASK_PERIOD_TASK5);
	task_init(TASK6_DATA_EXPORT, bll_dataExport, TASK_PERIOD_TASK6);
	task_init(TASK7_RTC, bll_RTC, TASK_PERIOD_TASK7);
//	task_init(TASK8_RGB, bll_RGB, TASK_PERIOD_TASK8);
	task_init(TASK9_BEEP, bll_beep, TASK_PERIOD_TASK9);
	task_init(TASK10_BACKLIGHT, bll_backlight, TASK_PERIOD_TASK10);

	/* 使能初始化任务 */
	task_start(TASK1_INIT);

	task_process();
}
/************** (C) COPYRIGHT Beijing WIIP Co., Ltd. ***********END OF FILE****/
